package cn.myzqu.sign.message.server.api;


import cn.myzqu.sign.message.client.MessageClient;
import cn.myzqu.sign.common.response.ResultData;
import cn.myzqu.sign.message.common.input.MessageInput;
import cn.myzqu.sign.message.server.base.page.Page;
import cn.myzqu.sign.message.server.entity.OsMessage;
import cn.myzqu.sign.message.server.service.MessageService;
import cn.myzqu.sign.message.server.util.TokenUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;


/**
 * @Author: 小柳
 * @Date: 2019/1/23 17:29
 */
@RestController
@Slf4j
@Validated
public class MessageApi implements MessageClient {

    @Autowired
    private MessageService messageService;


    @ApiOperation("新增系统通知（管理员或者系统业务需要）")
    @PostMapping("/info")
    @Override
    public ResultData addMessage(@RequestBody @Valid MessageInput messageInput) {

        OsMessage osMessage = new OsMessage();
        BeanUtils.copyProperties(messageInput, osMessage);
        boolean result = messageService.addMessage(osMessage);
        if (result) {
            return ResultData.success();
        } else {
            return ResultData.error("新增通知失败，请稍后重试");
        }

    }

    @ApiOperation("修改系统通知用户已读状态")
    @PutMapping("/readStatus/{id}")
    @Override
    public ResultData changeReadStatus(@PathVariable @NotNull(message = "id不能为空") Integer id) {
        boolean result = messageService.changeReadStatus(id);
        if (result) {
            return ResultData.success();
        } else {
            return ResultData.error("已读状态修改失败");
        }

    }

    @ApiOperation("修改系统通知用户可见状态")
    @PutMapping("/status/{id}/{status}")
    public ResultData changeStatus(@PathVariable Byte status,
                                      @PathVariable Integer id) {

        boolean result = messageService.changeStatus(status, id);
        if (result) {
            return ResultData.success();
        } else {
            return ResultData.error("可见状态修改失败");
        }

    }

    @ApiOperation("用户获取最新的消息列表")
    @GetMapping("/group")
    public ResultData getMessage(HttpServletRequest request ,
                                    @RequestParam(defaultValue = "1") Integer current,
                                    @RequestParam(defaultValue = "10" ) Integer size ) throws Exception{
        Integer userId = TokenUtil.getUserId(request);
        Page page = messageService.selectUserMessageGroupBySender(userId,current,size);
        return ResultData.success(page);
    }

    @ApiOperation("用户按发送者分类获取消息列表")
    @GetMapping("/listBySender")
    public ResultData getMessage(HttpServletRequest request ,
                                    @RequestParam Integer senderId,
                                    @RequestParam(defaultValue = "1") Integer current,
                                    @RequestParam(defaultValue = "10" ) Integer size ) throws Exception{
        Integer userId = TokenUtil.getUserId(request);
        Page page = messageService.selectUserMessageBySender(userId,senderId,current,size);
        return ResultData.success(page);
    }

    @ApiOperation("根据用户id获取分页系统通知")
    @GetMapping("/list/user")
    @Override
    public ResultData getMessageByUserId(@RequestParam Integer userId,
                                            @RequestParam(defaultValue = "1") Integer current,
                                            @RequestParam(defaultValue = "10" ) Integer size){

        Page<OsMessage> page = messageService.findByUserId(userId,current,size);
        return ResultData.success(page);
    }

}
